package org.demo.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;

import org.demo.dao.IOrderDao;
import org.demo.entity.Orders;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;



public class OrderDaoImpl extends HibernateDaoSupport implements IOrderDao {

	public List<Orders> findList(String cstName, String year) throws ParseException {

		String year1=year+"-01-01 00:00:00";
		String year2=year+"-12-31 23:59:59";
		StringBuffer hql =new StringBuffer("select o  from Orders o");
		 
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Orders> list=null;
		 
		if (year!=null && cstName==null) {
			
			hql.append(" where odrDate>= :year1 and odrDate<= :year2" );
			 list=getHibernateTemplate().findByNamedParam(hql.toString(), 
					new String[]{"year1","year2"}, new Object[]{dateFormat.parse(year1),dateFormat.parse(year2)});
 
		}else if (year==null && cstName!=null) {
			hql.append(" where odrCustomer like :odrCustomer" );
			 list=getHibernateTemplate().findByNamedParam(hql.toString(),"odrCustomer","%"+cstName+"%");

		}else if (year!=null && cstName!=null) {
			hql.append(" where odrCustomer like :odrCustomer and  odrDate>= :year1 and odrDate<= :year2" );
			 list=getHibernateTemplate().findByNamedParam(hql.toString(),new String[]{"odrCustomer","year1","year2"}, new Object[]{"%"+cstName+"%",dateFormat.parse(year1),dateFormat.parse(year2)});

		}
		else {
			 list=getHibernateTemplate().find(hql.toString());
		}
		 
 
		 
		return list;
	}
	
	

}
